'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 好友
 * @author sundbcn@gmail.com (sun debing)
 */

var mongoose = require('mongoose');
var moment = require('moment');
var _ = require('lodash');
var constantUtil = require('../util/constantUtil');
var friendItemSchema = require('./friend/friendItemSchema');
var friendMgr = require('../mgr/friendMgr');

///////////////////////////////////////////////////////////
/**
 * 申请好友
 * @type {mongoose}
 */
var requestFriendItemSchema = new mongoose.Schema({
    // 好友id
    role_id: {
        type: Number
    },

    // 申请时间
    create_time: {
        type: Date,
        default: Date.now
    }
});

if (!requestFriendItemSchema.options.toJSON) {
    requestFriendItemSchema.options.toJSON = {};
}
/* jshint unused:false */
requestFriendItemSchema.options.toJSON.transform = function (doc, ret) {
    delete ret.create_time;
    delete ret._id;
    delete ret.__v;
};

///////////////////////////////////////////////////////////
/**
 * 好友
 */
var friendSchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 好友列表
    friend_list: [friendItemSchema],

    // 请求的好友列表
    request_friend_list: [requestFriendItemSchema],

    // 今天好友点赞次数
    today_send_praise_times: {
        type: Number,
        default: 0
    },

    // 获取好友点赞次数
    today_receive_praise_times: {
        type: Number,
        default: 0
    },

    // 今天赠送好友体力次数
    today_send_present: {
        type: Number,
        default: 0
    },

    // 今天接收好友体力次数
    today_receive_present: {
        type: Number,
        default: 0
    },

    // 友情值
    friendship_value: {
        type: Number,
        default: 0
    },

    // 友情值获取上限次数
    friendship_num: {
        type: Number,
        default: 0
    },

    // 已推荐好友列表
    recommend_friend_list: [Number],

    // 上次刷新的时间
    last_refresh_time: {
        type: Date,
        default: 0
    }
});

friendSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 增加友情值
 */
friendSchema.methods.addFriendshipValue = function () {
    var friendship_value = constantUtil.value('get_friend_coin_num');
    if (this.today_send_present <= constantUtil.value('give_sp_times')) {
        this.friendship_value += friendship_value;
        return friendship_value;
    } else {
        return 0;
    }
};

/**
 * 扣除友情值
 */
friendSchema.methods.subFriendshipValue = function (value) {
    if (this.friendship_value >= value) {
        this.friendship_value -= value;
        return true;
    } else {
        return false;
    }
};

/**
 * 已推荐好友列表
 */
friendSchema.methods.getFriendRecommendList = function () {
    return this.recommend_friend_list;
};

/**
 * 增加已推荐好友
 */
friendSchema.methods.addFriendRecommendList = function (role_id) {
    if (_.indexOf(this.recommend_friend_list, role_id) == -1) {
        this.recommend_friend_list.push(role_id);
    }
};

/**
 * 是否已推荐过
 */
friendSchema.methods.isFriendRecommendList = function (role_id) {
    if (_.indexOf(this.recommend_friend_list, role_id) == -1) {
        return true;
    } else {
        return false;
    }
};

/**
 * 获取好友数量
 */
friendSchema.methods.getFriendNum = function () {
    return this.friend_list.length;
};

/**
 * 查询好友
 */
friendSchema.methods.getFriend = function (friend_role_id) {
    for (var i = this.friend_list.length - 1; i >= 0; i--) {
        var friend_item = this.friend_list[i];
        if (friend_item.role_id == friend_role_id) {
            return friend_item;
        }
    }
    return null;
};

/**
 * 获取好友列表
 */
friendSchema.methods.getFriendList = function () {
    var friend_role_id_list = [];
    for (var i = this.friend_list.length - 1; i >= 0; i--) {
        friend_role_id_list.push(this.friend_list[i].role_id);
    }
    return friend_role_id_list;
};

/**
 * 删除过期好友申请
 */
friendSchema.methods.deleteOutDateRequest = function () {
    for (var i = this.request_friend_list.length - 1; i >= 0; i--) {
        var request_item = this.request_friend_list[i];
        if (moment().diff(moment(request_item.create_time), 'd') >= 10) {
            request_item.remove();
        }
    }
};

/**
 * 添加好友
 */
friendSchema.methods.addFriend = function (friend_role_id) {
    var friend_item = this.getFriend(friend_role_id);
    if (!!friend_item) {
        return;
    }

    this.friend_list.push({
        role_id: friend_role_id
    });
};

/**
 * 删除好友
 */
friendSchema.methods.removeFriend = function (friend_role_id) {
    var friend_item = this.getFriend(friend_role_id);
    if (!friend_item) {
        return;
    }
    friend_item.remove();
};

/**
 * 判断是否已经申请
 */
friendSchema.methods.isFriendRequest = function (friend_role_id) {
    var request_list = this.getRequestList();
    return _.indexOf(request_list, friend_role_id) != -1;
};

/**
 * 获取全部好友邀请
 */
friendSchema.methods.getRequestList = function () {
    var request_list = [];
    for (var i = this.request_friend_list.length - 1; i >= 0; i--) {
        var request_list_item = this.request_friend_list[i];
        request_list.push(request_list_item.role_id);
    }
    return request_list;
};

/**
 * 同意好友邀请
 */
friendSchema.methods.acceptFriend = function (friend_role_id) {
    if (!this.isFriendRequest(friend_role_id)) {
        return false;
    }
    this.addFriend(friend_role_id);
    this.removeRequest(friend_role_id);
};

/**
 * 添加好友邀请
 * @param  {[type]} request_id [description]
 * @return {[type]}            [description]
 */
friendSchema.methods.addRequest = function (request_id) {
    var friend_item = this.getFriend(request_id);

    // 判断是否是好友
    if (!!friend_item) {
        return;
    }

    // 判断是否已经申请过
    if (this.isFriendRequest(request_id)) {
        return;
    }

    this.request_friend_list.push({
        role_id: request_id
    });
};

/**
 * 移除好友邀请
 * @param  {[type]} request_id [description]
 * @return {[type]}            [description]
 */
friendSchema.methods.removeRequest = function (request_id) {
    for (var i = this.request_friend_list.length - 1; i >= 0; i--) {
        var request_item = this.request_friend_list[i];
        if (request_item.role_id != request_id) {
            continue;
        }
        request_item.remove();
    }
};

/**
 * 一键删除好友申请
 */
friendSchema.methods.removeRequestList = function () {
    this.request_friend_list = [];
};

/**
 * 是否已经赠送过体力
 */
friendSchema.methods.isSendEnergy = function (friend_role_id) {
    var friend_item = this.getFriend(friend_role_id);
    if (!friend_item) {
        return false;
    }

    if (friend_item.is_send_energy) {
        return true;
    }
};

friendSchema.methods.isSendMax = function () {
    // 判断赠送体力次数是否超上限
    if (this.today_send_present >= constantUtil.value('give_sp_times')) {
        return true;
    }
    return false;
};

/**
 * 赠送体力
 */
friendSchema.methods.sendEnergy = function (friend_role_id) {
    if (!!this.isSendEnergy(friend_role_id)) {
        return false;
    }

    // 判断赠送体力次数是否超上限
    if (!!this.isSendMax()) {
        return false;
    }

    var friend_item = this.getFriend(friend_role_id);
    if (!friend_item) {
        return false;
    }
    this.today_send_present++;
    friend_item.is_send_energy = true;
    return true;
};

/**
 * 一键收取好友赠送体力
 */
friendSchema.methods.removeAllPresent = function () {
    var present_sp_times = 0;
    var present_energy_times = 0;

    for (var i = this.friend_list.length - 1; i >= 0; i--) {
        if (this.today_receive_present >= constantUtil.value('friend_sp_times')) {
            break;
        }

        if (this.friend_list[i].is_present_energy) {
            this.friend_list[i].is_received_energy = true;
            this.friend_list[i].is_present_energy = false;
            this.today_receive_present++;
            present_energy_times++;
        }
    }
    return present_energy_times;
};

/**
 * 当天已经接收的好友体力的次数
 * @return {[type]} [description]
 */
friendSchema.methods.getTodayReceiveNum = function () {
    return this.today_receive_present;
};

/**
 * 当天已经赠送的好友体力的次数
 * @return {[type]} [description]
 */
friendSchema.methods.getTodaySendNum = function () {
    return this.today_send_present;
};

/**
 * 添加好友赠送的体力
 */
friendSchema.methods.addPresent = function (friend_role_id) {
    var friend_item = this.getFriend(friend_role_id);

    // 判断好友是否存在
    if (!friend_item) {
        return false;
    }

    // 判断是否已经赠送过
    if (friend_item.is_present_energy) {
        return false;
    }

    friend_item.is_present_energy = true;
    return true;
};

/**
 * 收取体力
 */
friendSchema.methods.removePresent = function (friend_role_id) {
    // 判断是否超过领取次数
    if (this.today_receive_present >= constantUtil.value('friend_sp_times')) {
        return false;
    }

    var friend_item = this.getFriend(friend_role_id);

    // 判断好友是否存在
    if (!friend_item) {
        return false;
    }

    // 判断是否可以领取
    if (!friend_item.is_present_energy) {
        return false;
    }

    friend_item.is_received_energy = true;
    friend_item.is_present_energy = false;
    this.today_receive_present++;
    return true;
};

/**
 * 获取点赞好友列表
 */
friendSchema.methods.getPraiseList = function () {
    var today_praise_friend_list = [];
    for (var i = this.friend_list.length - 1; i >= 0; i--) {
        if (this.friend_list[i].is_send_praise) {
            today_praise_friend_list.push(this.friend_list[i].role_id);
        }
    }
    return today_praise_friend_list;
};

/**
 * 获取可接收点赞列表
 */
friendSchema.methods.getPresentPraiseList = function () {
    var present_praise_friend_list = [];
    for (var i = this.friend_list.length - 1; i >= 0; i--) {
        if (this.friend_list[i].is_present_praise) {
            present_praise_friend_list.push(this.friend_list[i].role_id);
        }
    }
    return present_praise_friend_list;
};

/**
 * 给好友点赞
 * @return {[type]} [description]
 */
friendSchema.methods.sendPraise = function (friend_role_id) {
    var friend_item = this.getFriend(friend_role_id);

    // 判断是否是朋友
    if (!friend_item) {
        return false;
    }

    //点赞是否已经超上限
    if (this.today_send_praise_times >= constantUtil.value('friend_enjoy_times')) {
        return false;
    }

    // 判断是否是已经被点赞
    if (friend_item.is_send_praise) {
        return false;
    }
    friend_item.is_send_praise = true;
    this.today_send_praise_times++;
    return true;
};

/**
 * 添加好友赠送的点赞
 * @return {[type]} [description]
 */
friendSchema.methods.addPraise = function (friend_role_id) {
    var friend_item = this.getFriend(friend_role_id);

    // 判断好友是否存在
    if (!friend_item) {
        return false;
    }

    if (friend_item.is_present_praise) {
        return false;
    }
    friend_item.is_present_praise = true;
    friend_item.is_today_praise = true;
    return true;
};

/**
 * 收取好友赠送的点赞
 * @return {[type]} [description]
 */
friendSchema.methods.receivePraise = function () {
    var praise_num = 0;

    // 判断收取好友点赞是否超上限
    for (var i = this.friend_list.length - 1; i >= 0; i--) {
        if (this.today_receive_praise_times * constantUtil.value('enjoy_get_point') >= constantUtil.value('enjoy_point_limit')) {
            break;
        }

        if (this.friend_list[i].is_present_praise) {
            this.friend_list[i].is_present_praise = false;
            this.today_receive_praise_times++;
            praise_num++;
        }
    }
    return praise_num;
};

/**
 * 获取接收好友点赞次数
 */
friendSchema.methods.getReceivePraiseNum = function () {
    return this.today_receive_praise_times;
};

/**
 * 上次刷新的时间
 * @return {[type]} [description]
 */
friendSchema.methods.getLastRefreshTime = function () {
    return this.last_refresh_time;
};

/**
 * 每日刷新
 * @return {[type]} [description]
 */
friendSchema.methods.refresh = function () {
    this.recommend_friend_list = [];
    for (var i = this.friend_list.length - 1; i >= 0; i--) {
        this.friend_list[i].is_present_praise = false;
        this.friend_list[i].is_present_energy = false;
        this.friend_list[i].is_send_praise = false;
        this.friend_list[i].is_today_praise = false;
        this.friend_list[i].is_send_energy = false;
        this.friend_list[i].is_received_energy = false;
    }
    this.today_receive_present = 0;
    this.today_send_present = 0;
    this.today_receive_praise_times = 0;
    this.today_send_praise_times = 0;
    this.last_refresh_time = moment();
    //this.friendship_num = constantUtil.value('get_friend_coin_limit');
};

/**
 * 获取修改
 * @return {[type]} [description]
 */
friendSchema.methods.getLastChange = function () {
    return this.toJSON();
};

/**
 * 推送给用户
 * @return {[type]} [description]
 */
friendSchema.methods.push = function () {
    this.pushModify();
};

friendSchema.methods.pushModify = function () {
    friendMgr.sendFriendChanged(
        this.role_id,
        this.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send friend message error: %j, fail ids: %j', err, fails);
                return;
            }
        }
    );
};

if (!friendSchema.options.toJSON) {
    friendSchema.options.toJSON = {};
}
/* jshint unused:false */
friendSchema.options.toJSON.transform = function (doc, ret) {
    delete ret.last_refresh_time;
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Friend', friendSchema);
